package cn.jekun.web.agent.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.D;
import cn.jekun.component.base.validator.annotation.NotBlank;
import cn.jekun.component.base.validator.annotation.Num;
import cn.jekun.component.core.bean.Agent;
import cn.jekun.component.core.bean.gen.RsAgent.AnnoAgentId;
import cn.jekun.component.core.bean.gen.SgAccount;
import cn.jekun.component.core.bean.gen.TsAccount;
import cn.jekun.component.core.bean.gen.TsAccountJournal;
import cn.jekun.component.core.bean.gen.TsOperationCode;
//import cn.jekun.component.core.dao.AccountDao;

/**
 * 账户流水查询Service
 *
 */
@Service(AccountService.BEAN_NAME)
public class AccountService extends AgentBaseService{
	public static final String BEAN_NAME="cn.jekun.web.agent.service.AccountService";
//	@Resource(name=AccountDao.BEAN_NAME)
//	protected AccountDao accountDao;
	@Override
	public String getNamespace() {
		// TODO Auto-generated method stub
		return "ACTION_AGENT_TS_ACCOUNT";
	}
  
//	/**
//	 * 获取余额
//	 * @param ShopId
//	 * @return
//	 */
//	public Double getBalance(String agentId){
//		try {
//			return accountDao.getAgentBalance(agentId);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}

	/**
	 * 获取账户流水明细
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List<Agent> getAccountDetails(
			String queryDate,
			String operType,
			@Num(length=8,scale=4,message="金额的格式不正确")String beginMoney,
			@Num(length=8,scale=4,message="金额的格式不正确")String endMoney,
			String keyRange,
		    @D(message="开始日期格式不对")String beginDate,
		    @D(message="结束日期格式不对")String endDate,
		    String keyword_s,
		    @NotBlank(message="代理商代码不能为空")@AnnoAgentId String agentId,
		    Page page){
		
        Map map = new HashMap();
        map.put("queryDate", queryDate);
		map.put("operType", operType);
		map.put("beginMoney", beginMoney);
		map.put("endMoney",endMoney);
		map.put("keyRange",keyRange);
		map.put("beginDate",beginDate);
		map.put("endDate",endDate);
		map.put("keyword_s",keyword_s);
		map.put("agentId", agentId);
		String SQL ="";
		if(map.get("queryDate")!=null && map.get("queryDate").toString().length()>0){
			if("1".equals(map.get("queryDate"))){ 
				SQL +=" and trunc(d.CREATE_TIME) = trunc(sysdate)";  //今天的数据
			}else if("2".equals(map.get("queryDate"))){
				SQL +=" and trunc(d.create_time) >= trunc(add_months(sysdate,-1))"; //最近一个月的数据
			}else if(map.get("queryDate")!=null && "3".equals(map.get("queryDate"))){
				SQL +=" and trunc(d.create_time) >= trunc(add_months(sysdate,-3))";  //3个月的数据
			}else if("4".equals(map.get("queryDate"))){
				SQL +=" and trunc(d.create_time) >= trunc(add_months(sysdate,-12))"; //1年的数据
			}else if("5".equals(map.get("queryDate"))){ //1年前的数据
				SQL +=" and trunc(d.create_time) <= trunc(add_months(sysdate,-12))";	
			}
		}if(map.get("operType")!=null && map.get("operType").toString().length()>0){
			SQL +=" and e.OPERATION_CODE = '"+map.get("operType").toString()+"'"; 
		}if(!"".equals(map.get("beginMoney")) && "".equals(map.get("endMoney"))){
			SQL +=" and d.amount>="+map.get("beginMoney"); 
		}if("".equals(map.get("beginMoney")) && !"".equals(map.get("endMoney"))){
			SQL +=" and d.amount<="+map.get("endMoney"); 
		}if(map.get("beginMoney")!=null && map.get("endMoney")!=null && !"".equals(map.get("beginMoney")) 
				&& !"".equals(map.get("endMoney"))){
			SQL +=" and d.amount>="+map.get("beginMoney")+" and d.amount<="+map.get("endMoney")+""; 
		}if("".equals(map.get("beginDate")) && !"".equals(map.get("endDate"))){
			SQL +=" and d.create_time"
					+ "<= to_date('"+map.get("endDate").toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(map.get("beginDate")) && "".equals(map.get("endDate"))){
			SQL +=" and d.create_time"
					+ ">= to_date('"+map.get("beginDate").toString().trim()+"','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(map.get("beginDate")) && map.get("beginDate")!=null 
				&& !"".equals(map.get("endDate")) && map.get("endDate")!=null){
			SQL +=" and d.create_time >= to_date('"+map.get("beginDate").toString().trim()+"','yyyy/MM/dd hh24:mi:ss')"
					+ " and d.create_time <= to_date('"+map.get("endDate").toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')"; 
		}if(map.get("keyRange")!=null && map.get("keyword_s").toString().length()>0 ){
			if("bill".equals(map.get("keyRange"))){
				SQL +=" and d.account_journal_id like '%"+map.get("keyword_s").toString()+"%'"; 	
			}else if("name".equals(map.get("keyRange"))){
				SQL +=" and e.operation like '%"+map.get("keyword_s").toString()+"%'"; 
			}

		}
		SqlUtis.prepareSql(map,SQL);
		return this.list(this.getStatementName("list"),map,page);

	}

	/**
	 * 获取账户流水明细
	 * @param map
	 * @return
	 
	@SuppressWarnings("rawtypes")
	public List<Agent> getAccountDetails(Map map,Page page){
		String SQL ="";
		if(map.get("queryDate")!=null && map.get("queryDate").toString().length()>0){
			if("1".equals(map.get("queryDate"))){ 
				SQL +=" and trunc(d.CREATE_TIME) = trunc(sysdate)";  //今天的数据
			}else if("2".equals(map.get("queryDate"))){
				SQL +=" and trunc(d.create_time) >= trunc(add_months(sysdate,-1))"; //最近一个月的数据
			}else if(map.get("queryDate")!=null && "3".equals(map.get("queryDate"))){
				SQL +=" and trunc(d.create_time) >= trunc(add_months(sysdate,-3))";  //3个月的数据
			}else if("4".equals(map.get("queryDate"))){
				SQL +=" and trunc(d.create_time) >= trunc(add_months(sysdate,-12))"; //1年的数据
			}else if("5".equals(map.get("queryDate"))){ //1年前的数据
				SQL +=" and trunc(d.create_time) <= trunc(add_months(sysdate,-12))";	
			}
		}if(map.get("operType")!=null && map.get("operType").toString().length()>0){
			SQL +=" and e.OPERATION_CODE = '"+map.get("operType").toString()+"'"; 
		}if(!"".equals(map.get("beginMoney")) && "".equals(map.get("endMoney"))){
			SQL +=" and d.amount>="+map.get("beginMoney"); 
		}if("".equals(map.get("beginMoney")) && !"".equals(map.get("endMoney"))){
			SQL +=" and d.amount<="+map.get("endMoney"); 
		}if(map.get("beginMoney")!=null && map.get("endMoney")!=null && !"".equals(map.get("beginMoney")) 
				&& !"".equals(map.get("endMoney"))){
			SQL +=" and d.amount>="+map.get("beginMoney")+" and d.amount<="+map.get("endMoney")+""; 
		}if("".equals(map.get("beginDate")) && !"".equals(map.get("endDate"))){
			SQL +=" and d.create_time"
					+ "<= to_date('"+map.get("endDate").toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(map.get("beginDate")) && "".equals(map.get("endDate"))){
			SQL +=" and d.create_time"
					+ ">= to_date('"+map.get("beginDate").toString().trim()+"','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(map.get("beginDate")) && map.get("beginDate")!=null 
				&& !"".equals(map.get("endDate")) && map.get("endDate")!=null){
			SQL +=" and d.create_time >= to_date('"+map.get("beginDate").toString().trim()+"','yyyy/MM/dd hh24:mi:ss')"
					+ " and d.create_time <= to_date('"+map.get("endDate").toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')"; 
		}if(map.get("keyRange")!=null && map.get("keyword_s").toString().length()>0 ){
			if("bill".equals(map.get("keyRange"))){
				SQL +=" and d.account_journal_id like '%"+map.get("keyword_s").toString()+"%'"; 	
			}else if("name".equals(map.get("keyRange"))){
				SQL +=" and e.operation like '%"+map.get("keyword_s").toString()+"%'"; 
			}

		}
		SqlUtis.prepareSql(map,SQL);
		return this.list(this.getStatementName("list"),map,page);

	}
*/
	/**
	 * 获取账户流水类型
	 * @return
	 */
	public List<TsOperationCode> getOperTypes(){

		return this.list(this.getStatementName("queryoperationcode"),null);
	}

	/**
	 * 获取日期条件
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Agent> getQueryDate(String queryDate,String operType,@Num(length=8,scale=4,message="金额的格式不正确")String beginMoney,@Num(length=8,scale=4,message="金额的格式不正确")String endMoney,String keyRange,
		      @D(message="开始日期格式不对")String beginDate,@D(message="结束日期格式不对")String endDate,String keyword_s,@NotBlank(message="代理商代码不能为空")@AnnoAgentId String agentId){
		
		Map map = new HashMap();
        map.put("queryDate", queryDate);
		map.put("operType", operType);
		map.put("beginMoney", beginMoney);
		map.put("endMoney",endMoney);
		map.put("keyRange",keyRange);
		map.put("beginDate",beginDate);
		map.put("endDate",endDate);
		map.put("keyword_s",keyword_s);
		map.put("agentId", agentId);
		
		String SQL ="";
		if(map.get("queryDate")!=null && map.get("queryDate").toString().length()>0){
			if("1".equals(map.get("queryDate"))){ 
				SQL +=" to_char(sysdate,'yyyy-MM-dd') beginDate,to_char(sysdate,'yyyy-MM-dd') endDate";  //今天
			}else if("2".equals(map.get("queryDate"))){
				SQL +="to_char(add_months(sysdate,-1),'yyyy-MM-dd') beginDate, to_char(sysdate,'yyyy-MM-dd') endDate"; //最近一个月
			}else if(map.get("queryDate")!=null && "3".equals(map.get("queryDate"))){
				SQL +="to_char(add_months(sysdate,-3),'yyyy-MM-dd') beginDate, to_char(sysdate,'yyyy-MM-dd') endDate";  //3个月
			}else if("4".equals(map.get("queryDate"))){
				SQL +="to_char(add_months(sysdate,-12),'yyyy-MM-dd') beginDate,to_char(sysdate,'yyyy-MM-dd') endDate"; //1年
			}else if("5".equals(map.get("queryDate"))){ //1年前
				SQL +="to_char(add_months(sysdate,-12),'yyyy-MM-dd') endDate";	
			}
			map.put("COLUMN_SQL", SQL);
			return this.list(this.getStatementName("getQueryDate"),map);
		}else{
			SQL += "to_char(min(create_time),'yyyy-MM-dd') beginDate,to_char(max(create_time),'yyyy-MM-dd') endDate";
			map.put("COLUMN_SQL", SQL);
			return this.list(this.getStatementName("getQueryDate2"),map);
		}

	}

	/**
	 * 获取账户提现详情
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public cn.jekun.web.agent.bean.Agent getAccountDetailsById(@TsAccountJournal.AnnoAccountJournalId()String accountJourId){
		Map map = new HashMap();
		map.put("accountJourId", accountJourId);
		return this.one(this.getStatementName("getAccountsDetailsById"),map);
	}
    
	/**
	 *  查询账户金额
	 * @param statementName
	 * @param agentId
	 * @return
	 */
	public TsAccount getAccountByOne(@NotBlank(message="调用xml文件的Id不能为空")String statementName,@NotBlank(message="代理商代码不能为空")@AnnoAgentId String agentId){
		Map map = new HashMap();
		map.put("agentId", agentId);
		return this.one(this.getStatementName(statementName),map);
	}
	
	/**
	 * 获取当天提现的次数
	 * @param accountId
	 * @return
	 */
	public int getSettleTimes(@NotBlank(message="账户代码不能为空")@SgAccount.AnnoAccountId String accountId){
		Map map = new HashMap();
		map.put(SgAccount.COL_ACCOUNT_ID, accountId);
		return this.count(this.getStatementName("getSettleTimes"),map);
	}
}
